<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingAlarm;
use App\Models\DedustingDevicePoint;
use App\Models\DedustingDevice;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use App\Handlers\IotCardHandler;

class AlarmController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('报警设置列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(DedustingAlarm::with(['device']), function (Grid $grid) {
            $grid->column('device.name', '所属设备');
            $grid->column('name','报警名称');
            $grid->column('point', '采集点')->using(DedustingDevicePoint::POINTS);
            $grid->column('condition', '条件')->using(DedustingAlarm::CONDITION_TEXT);
            $grid->column('value','报警值');
            $grid->column('level', '报警等级')->using(DedustingAlarm::LEVEL_TEXT);
            $grid->column('active', '状态')->using(DedustingAlarm::ACTIVE_TEXT);
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
        });
    }

    public function show($id, Content $content)
    {
        return $content->header('报警设置')
            ->description('详情')
            ->body(Show::make($id, DedustingAlarm::with(['device']), function (Show $show) {
                $show->field('device.name', '所属设备');
                $show->field('name','报警名称');
                $show->field('point', '采集点')->using(DedustingDevicePoint::POINTS);
                $show->field('condition', '条件')->using(DedustingAlarm::CONDITION_TEXT);
                $show->field('value','报警值');
                $show->field('level', '报警等级')->using(DedustingAlarm::LEVEL_TEXT);
                $show->field('active', '状态')->using(DedustingAlarm::ACTIVE_TEXT);
                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建报警设置')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingAlarm(), function (Form $form) {
            $devices = DedustingDevice::pluck('name','id');
            $form->select('device_id', '设备')->options($devices)->required();
            $form->text('name', '报警名称')->required();
            $form->select('point', '采集点')->options(DedustingDevicePoint::POINTS);
            $form->radio('condition', '条件')->options(DedustingAlarm::CONDITION_TEXT)->required();
            $form->text('value', '报警值')->required();
            $form->radio('level', '报警等级')->options(DedustingAlarm::LEVEL_TEXT)->default(1);
            $form->select('library_id', '关联文库')->options('/dedusting/api/libraries');
            $form->switch('active', '是否启用')->default(1);

            //处理缓存
            $form->saved(function (Form $form, $result) {
                if ($form->active) {
                    $cacheData = [
                        'device_id' => $form->device_id,
                        'point' => $form->point,
                        'alarm_id' => $form->getKey(),
                        'alarm_name' => $form->name,
                        'alarm_level' => $form->level,
                        'alarm_value' => $form->value,
                        'library_id' => $form->library_id,
                    ];
                    DedustingAlarm::setCache($cacheData, $form->condition);
                }
                return;
            });

            $form->deleted(function (Form $form, $result) {
                $data = $form->model()->toArray();
                if (! $result) {
                    return $form->response()->error('数据删除失败');
                }

                foreach ($data as $one) {
                    DedustingAlarm::delCache($one['device_id'], $one['point'], $one['condition']);
                }
                return;
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('报警设置编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}